package com.cqhilink.iot.jobstandardmanagement.services.innerservice.impl.pipeline;/**
 * Created by Administrator on 2017/3/13.
 */

import com.alibaba.fastjson.JSON;
import com.cqhilink.api.common.utils.UniqueUtil;
import com.cqhilink.api.common.utils.enums.RespCodeEnum;
import com.cqhilink.iot.jobstandardmanagement.facade.utils.BackMessage;
import com.cqhilink.iot.jobstandardmanagement.dao.mapper.PipelineMapper;
import com.cqhilink.iot.jobstandardmanagement.facade.model.*;
import com.cqhilink.iot.jobstandardmanagement.facade.service.organization.JsmOrganizationService;
import com.cqhilink.iot.jobstandardmanagement.services.innerservice.util.OrgLevelUtils;
import com.cqhilink.iot.jobstandardmanagement.facade.service.pipeline.JsmPipelineService;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author 卢曼成
 * @Description 管线接口实现类
 * @CreateDateTime 2017-03-13 11:37
 **/

public class JsmPipelineServiceImpl implements JsmPipelineService {
    private Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    private PipelineMapper pipelineMapper;
    @Autowired
    private JsmOrganizationService organizationService;

    /**
     * 根据Id物理删除
     *
     * @param eqptId
     * @return int
     * @Author 卢曼成
     */
    @Override
    public int deleteByPrimaryKey(String eqptId) {
        return pipelineMapper.deleteByPrimaryKey(eqptId);
    }

    /**
     * 根据Id逻辑批量删除
     *
     * @param eqptId
     * @return int
     * @Author 卢曼成
     */
    @Override
    public Map<String, Object> deleteKeys(String eqptId) {
        if (StringUtils.isEmpty(eqptId)) {
            throw new RuntimeException("参数出错");
        }
        List<String> list = JSON.parseArray(eqptId, String.class);
        Map<String, Object> rowMap = Maps.newHashMap();
        try {
            int rowCount = pipelineMapper.deleteListKey(list);
            if (rowCount > 0) {
                logger.debug("删除管线:{}成功", list);
                rowMap.put("state", RespCodeEnum.RESP_CODE_ENUM_true.code());
                rowMap.put("msg", BackMessage.DEL_OK);
            } else {
                logger.debug("删除管线:{}失败", list);
                rowMap.put("state", RespCodeEnum.RESP_CODE_ENUM_false.code());
                rowMap.put("msg", BackMessage.DEL_FAIL);
            }
        } catch (Exception e) {
            logger.error("删除管线{}失败", list);
            throw new RuntimeException("删除阀室阀井");
        }
        return rowMap;
    }

    /**
     * 根据ID逻辑删除
     *
     * @param eqptId
     * @return int
     * @Author 卢曼成
     */
    @Override
    public int updateStatus(String eqptId) {
        return pipelineMapper.updateStatus(eqptId);
    }

    /**
     * 新增
     *
     * @param record
     * @return int
     * @Author 卢曼成
     */
    @Override
    public int insert(Pipeline record) {
        return pipelineMapper.insert(record);
    }

    /**
     * 非空新增
     *
     * @param record
     * @return int
     * @Author 卢曼成
     */
    @Override
    public int insertSelective(Pipeline record) {
        return pipelineMapper.insertSelective(record);
    }

    /**
     * 根据ID查询
     *
     * @param eqptId
     * @return Pipeline
     * @Author 卢曼成
     */
    @Override
    public Pipeline selectByPrimaryKey(String eqptId) {
        return pipelineMapper.selectByPrimaryKey(eqptId);
    }

    /**
     * 根据ID非空修改
     *
     * @param record
     * @return int
     * @Author 卢曼成
     */
    @Override
    public int updateByPrimaryKeySelective(Pipeline record) {
        return pipelineMapper.updateByPrimaryKeySelective(record);
    }

    /**
     * 修改
     *
     * @param record
     * @return int
     * @Author 卢曼成
     */
    @Override
    public int updateByPrimaryKey(Pipeline record) {
        return pipelineMapper.updateByPrimaryKey(record);
    }

    /**
     * 根据条件查询集合
     *
     * @param record
     * @return List<Pipeline>
     * @Author 卢曼成
     */
    @Override
    public List<Pipeline> selectByPrimaryList(Pipeline record) {
        return pipelineMapper.selectByPrimaryList(record);
    }

    /**
     * @Author 卢曼成
     * @Description 添加修改
     * @Date 2017/3/9 15:23
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public Map<String, Object> save(Pipeline record) throws Exception {
        if (record == null) {
            throw new RuntimeException("参数出错");
        }
        Map<String, Object> rowMap = Maps.newHashMap();
        Date date = new Date();
        record.setEqptId(UniqueUtil.uuid());
        record.setCreateDate(date);
        record.setUpdateDate(date);
        record.setBsflag("0");
        try {
            int rowCount = pipelineMapper.insert(record);
            if (rowCount > 0) {
                logger.debug("保存管线:{}成功", record);
                rowMap.put("state", RespCodeEnum.RESP_CODE_ENUM_true.code());
                rowMap.put("msg", BackMessage.ADD_OK);
            } else {
                logger.debug("保存管线:{}失败", record);
                rowMap.put("state", RespCodeEnum.RESP_CODE_ENUM_false.code());
                rowMap.put("msg", BackMessage.ADD_FAIL);
            }
        } catch (Exception e) {
            logger.error("保存管线{}失败", record);
            throw new RuntimeException("保存管线异常:" + e.getMessage());
        }
        return rowMap;
    }

    /**
     * 修改
     *
     * @param record
     * @return int
     * @Author 卢曼成
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public Map<String, Object> update(Pipeline record) throws Exception {
        if (record == null) {
            throw new IllegalArgumentException("参数出错");
        }
        Map<String, Object> rowMap = Maps.newHashMap();
        record.setUpdateDate(new Date());
        try {
            int rowCount = pipelineMapper.updateByPrimaryKeySelective(record);
            if (rowCount > 0) {
                logger.debug("修改管线:{}成功", record);
                rowMap.put("state", RespCodeEnum.RESP_CODE_ENUM_true.code());
                rowMap.put("msg", BackMessage.MODIFY_OK);
            } else {
                logger.debug("修改管线:{}失败", record);
                rowMap.put("state", RespCodeEnum.RESP_CODE_ENUM_false.code());
                rowMap.put("msg", BackMessage.MODIFY_FAIL);
            }
        } catch (Exception e) {
            logger.error("修改管线{}失败", record);
            e.printStackTrace();
            throw new RuntimeException("修改管线异常:" + e.getMessage());
        }
        return rowMap;
    }

    /**
     * @param list
     * @Author 卢曼成
     * @Description 批量添加
     * @Date 2017/2/8 17:17
     */
    @Override
    public int insertList(List<Pipeline> list) {
        return pipelineMapper.insertList(list);
    }

    /**
     * @param list
     * @Author 卢曼成
     * @Description 批量修改
     * @Date 2017/2/8 17:17
     */
    @Override
    public int updateList(List<Pipeline> list) {
        return pipelineMapper.updateList(list);
    }

    /**
     * @param list
     * @Author 卢曼成
     * @Description 批量逻辑删除
     * @Date 2017/2/8 17:18
     */
    @Override
    public int deleteList(List<Pipeline> list) {
        return pipelineMapper.deleteList(list);
    }

    /**
     * @param orgId
     * @param pOrgId
     * @param keyword
     * @return List<Ztree>
     * @Author 卢曼成
     * @Description 查询树形
     * @CreateDateTime 2017/5/23 15:01
     */
    @Override
    public List<Ztree> initTree(String orgId, String pOrgId, String keyword) {
        return organizationService.initTree(orgId, pOrgId, keyword, OrgLevelUtils.ORG_LEVEL_WELL);
    }


    /**
     * @param record
     * @return Map<String, Object>
     * @Author 卢曼成
     * @Description 分页查询
     * @CreateDateTime 2017/5/23 15:01
     */
    @Override
    public Map<String, Object> getPageList(Pipeline record) {
        Map<String, Object> resultMap = Maps.newHashMap();
        if (StringUtils.isEmpty(record.getOrgId())) {
            return resultMap;
        }
        List<Pipeline> list = pipelineMapper.queryList(record);
        int count = pipelineMapper.queryCount(record);
        resultMap.put("rows", list == null ? Lists.newArrayList() : list);
        resultMap.put("total", count);
        return resultMap;
    }
}
